package com.welike.shibo.controller.v2.api;

import com.alibaba.fastjson.JSONObject;
import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.service.RedisService;
import com.welike.shibo.service.UploadService;
import com.welike.shibo.utils.CommonUtil;
import com.welike.shibo.utils.ResultUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2019/12/30 3:07 下午
 */
@Slf4j
@Api(value = "首页相关接口")
@RestController
@RequestMapping("/v2/index")
public class HomeController {

    @Resource
    private UploadService uploadService;

    @Resource
    private RedisService redisService;

    @PostMapping("/version")
    public JsonResult setAndroidVersion(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "version, url, message");
        JSONObject version = new JSONObject();
        version.put("version", requestJson.getString("version"));
        version.put("url", requestJson.getString("url"));
        version.put("message", requestJson.getString("message"));
        redisService.set("version", version.toJSONString());
        return ResultUtils.success(version);
    }

    @PostMapping("/upload_apk")
    public JsonResult uploadApk(@RequestParam("file") MultipartFile apk) throws IOException {
        String apkUrl = uploadService.uploadFile(apk);
        String onlineUrl = "http://shortvideo.healthplatform.xyz/" + apkUrl;
        Map<String, Object> result = new HashMap<>();
        result.put("apk", onlineUrl);
        return ResultUtils.success(result);
    }
}
